package com.funmi.lizip.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.common.base.ResultCode;
import com.funmi.lizip.entity.Patrol;
import com.funmi.lizip.entity.PatrolTrack;
import com.funmi.lizip.entity.Personnel;
import com.funmi.lizip.entity.dto.patrol.PatrolQueryRequest;
import com.funmi.lizip.entity.vo.PatrolQueryVo;
import com.funmi.lizip.entity.vo.PatrolVo;
import com.funmi.lizip.mapper.PatrolMapper;
import com.funmi.lizip.service.PatrolService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.funmi.lizip.service.PatrolTrackService;
import com.funmi.lizip.service.PersonnelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yeyg
 * @since 2022-11-03
 */
@Service
public class PatrolServiceImpl extends ServiceImpl<PatrolMapper, Patrol> implements PatrolService {

    @Autowired
    private PatrolMapper patrolMapper;
    @Autowired
    private PatrolTrackService patrolTrackService;

    /**
     * 根据巡护人员id 获取当前人员正在执行的巡护任务集合
     * @param id
     * @return
     */
    @Override
    public List<Integer> getByRangerId(int id) {
        // 获取正在执行的巡护任务
        List<Integer> ids = patrolMapper.getIds();
        if (ids == null){
            R.success().code(ResultCode.PATROL_NOT_EXIST);
            return null;
        }
        ArrayList<Integer> list = new ArrayList<>();
        ids.forEach(patrolId -> {
            List<Integer> rangerIds = patrolMapper.getRangerIds(patrolId);
            rangerIds.forEach(rangerId -> {
                if (rangerId == null && rangerId == id) {
                    list.add(patrolId);
                }
            });
        });
        // 生成返回值
        return list;

    }


    @Override
    public String getName(String rangerIds) {

        return null;
    }

    @Override
    public String getTrack(Integer id) {
        return null;
    }


    @Override
    public Page<PatrolQueryVo> getPatrol(PatrolQueryRequest request) {
        long page = 1L;
        long pageSize = 10L;
        if (request != null){
            page = request.getPage();
            pageSize = request.getPageSize();
        }
        QueryWrapper<Patrol> patrolWrapper = new QueryWrapper<>();
        patrolWrapper.eq("is_delete",0);
        // 获取巡护任务
        List<Patrol> patrols = patrolMapper.selectList(patrolWrapper);
        List<PatrolQueryVo> patrolQueryVos = new ArrayList<>();
        // 查询出巡护员ids
        patrols.forEach(patrol -> {
            String rangerIds = patrol.getRangerIds().toJSONString();
            Map<Integer, String> map = JSONObject.parseArray(rangerIds).toJavaList(JSONObject.class)
                    .stream().filter(Objects::nonNull).collect(Collectors.toMap(
                            item -> item.getInteger("id"),
                            item -> item.getString("name")));
            map.forEach((id, name) -> {
                // 获取巡护轨迹
                PatrolTrack track = patrolTrackService.getByRangerId(id);
                Personnel personnel = new Personnel(id, name);
                patrolQueryVos.add(new PatrolQueryVo(patrol, track, personnel));
            });
        });
        PageDTO<PatrolQueryVo> patrolQueryVoPageDTO = new PageDTO<>(page, pageSize, patrolQueryVos.size());
        return patrolQueryVoPageDTO.setRecords(patrolQueryVos);
    }
}
